package com.example.springcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.springcloud.domain.Comments;
import com.example.springcloud.domain.Images;
import com.example.springcloud.mapper.*;
import com.example.springcloud.request.PublishRequest;
import com.example.springcloud.vo.CommentsVo;
import com.example.springcloud.vo.PostsVo;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springcloud.domain.Posts;
import com.example.springcloud.service.IPostsService;

/**
 * 用户种草内容Service业务层处理
 * 
 * @author hx
 * @date 2025-02-14
 */
@Service
public class PostsServiceImpl implements IPostsService 
{
    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private CommentsMapper commentsMapper;


    /**
     * 查询用户种草内容
     * 
     * @param postId 用户种草内容主键
     * @return 用户种草内容
     */
    @Override
    public PostsVo selectPostsByPostId(Long postId)
    {

        Posts post = postsMapper.selectPostsByPostId(postId);
        PostsVo postsVo = new PostsVo();
        BeanUtils.copyProperties(post, postsVo);
        // 图片集合
        postsVo.setImgList(imagesMapper.selectImagesByPostId(post.getPostId()));
        // 评论集合
        List<Comments> commentsList = commentsMapper.selectCommentsByPostId(post.getPostId());
        List<CommentsVo> commentsVoList = new ArrayList<>();
        commentsList.forEach(comment -> {
            CommentsVo commentsVo = new CommentsVo();
            commentsVo.setContent(comment.getContent());
            commentsVo.setTime(comment.getCreatedAt());
            // 用户信息
            commentsVo.setAuthorName(usersMapper.selectUsersByUserId(comment.getUserId()).getNickname());
            commentsVo.setAvatar(usersMapper.selectUsersByUserId(comment.getUserId()).getAvatar());
            commentsVoList.add(commentsVo);
        });
        postsVo.setComments(commentsVoList);
        return postsVo;
    }

    /**
     * 查询用户种草内容列表
     * 
     * @param posts 用户种草内容
     * @return 用户种草内容
     */
    @Override
    public List<PostsVo> selectPostsList(Posts posts)
    {
        List<Posts> postsList = postsMapper.selectPostsList(posts);
        List<PostsVo> postsVoList = new ArrayList<>();
        postsList.forEach(post -> {
            PostsVo postsVo = new PostsVo();
            BeanUtils.copyProperties(post, postsVo);
            postsVo.setCategoryType(post.getCategoryType());
            // 图片集合
            postsVo.setImgList(imagesMapper.selectImagesByPostId(post.getPostId()));
            // 评论集合
            List<Comments> commentsList = commentsMapper.selectCommentsByPostId(post.getPostId());
            List<CommentsVo> commentsVoList = new ArrayList<>();
            commentsList.forEach(comment -> {
                CommentsVo commentsVo = new CommentsVo();
                commentsVo.setContent(comment.getContent());
                commentsVo.setTime(comment.getCreatedAt());
                // 用户信息
                commentsVo.setAuthorName(usersMapper.selectUsersByUserId(comment.getUserId()).getNickname());
                commentsVo.setAvatar(usersMapper.selectUsersByUserId(comment.getUserId()).getAvatar());
                commentsVoList.add(commentsVo);
            });
            postsVo.setComments(commentsVoList);
            postsVoList.add(postsVo);
        });
        return postsVoList;
    }

    /**
     * 新增用户种草内容
     * 
     * @param posts 用户种草内容
     * @return 结果
     */
    @Override
    public int insertPosts(Posts posts)
    {
        return postsMapper.insertPosts(posts);
    }

    /**
     * 修改用户种草内容
     * 
     * @param posts 用户种草内容
     * @return 结果
     */
    @Override
    public int updatePosts(Posts posts)
    {
        return postsMapper.updatePosts(posts);
    }

    /**
     * 批量删除用户种草内容
     * 
     * @param postIds 需要删除的用户种草内容主键
     * @return 结果
     */
    @Override
    public int deletePostsByPostIds(Long[] postIds)
    {
        return postsMapper.deletePostsByPostIds(postIds);
    }

    /**
     * 删除用户种草内容信息
     * 
     * @param postId 用户种草内容主键
     * @return 结果
     */
    @Override
    public int deletePostsByPostId(Long postId)
    {
        return postsMapper.deletePostsByPostId(postId);
    }

    /**
     * 用户发布种草文章
     */
    @Override
    public AjaxResult publishPosts(PublishRequest publishRequest) {
        Posts posts = new Posts();
        BeanUtils.copyProperties(publishRequest, posts);
        // 发布时间
        posts.setCategoryType(publishRequest.getCategoryType());
        posts.setCreatedAt(new Date());
        Long postId = (long) postsMapper.insertPosts(posts);
        // 图片集合
        publishRequest.getImgList().forEach(file -> {
            Images images = new Images();
            images.setEntityType(2);
            images.setEntityId(posts.getPostId());
            images.setImageUrl(file);
            imagesMapper.insertImages(images);
        });
        return AjaxResult.success("发布成功");
    }
}
